import logging
import sys
from datetime import datetime

from loguru import logger as _logger

from app.config import PROJECT_ROOT


def define_log_level(print_level="INFO", logfile_level="DEBUG", name: str = None):
    """Adjust the log level to above level"""

    # 设置第三方库日志级别
    logging.getLogger('langchain_mcp_adapters').setLevel(logging.WARNING)
    logging.getLogger('mcp').setLevel(logging.WARNING)

    # 设置业务日志
    formatted_date = datetime.now().strftime("%Y%m%d")
    log_name = f"{name}_{formatted_date}" if name else formatted_date

    _logger.remove()  # 移除所有已配置的日志处理器
    _logger.add(sys.stderr, level=print_level, backtrace=False)
    _logger.add(PROJECT_ROOT / f"logs/{log_name}.log", level=logfile_level)
    return _logger


logger = define_log_level("WARNING")
